package com.ycsd.invest.order.business.apiplugin;


import com.ycsd.invest.order.business.dto.StudentInfoDto;
import kd.bos.coderule.api.CodeRuleInfo;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiMapping;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.custom.annotation.ApiRequestBody;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.io.Serializable;

@ApiController(value = "studentApi", desc = "学生api测试用例")
@ApiMapping("/student")
public class StudentController implements Serializable {

    /**
     * 通过id获取学生信息
     *
     * @param id
     * @return
     */
//    @ApiGetMapping("/get")
//    public CustomApiResult<StudentInfoVo> getStudentInfo(@ApiParam(value = "学生id", required = true) Long id) {
//        DynamicObject dynamicObject = QueryServiceHelper.queryOne(
//                "ycsd_student_info"
//                , "id,name"
//                , new QFilter[]{
//                        new QFilter("id", QCP.equals, id)});
//        return CustomApiResult.success(
//                new StudentInfoVo(dynamicObject.getLong("id")
//                        , dynamicObject.getString("name")));
//    }

    /**
     * 写入学生信息
     *
     * @param dto
     * @return
     */
    @Validated({StudentInfoDto.Save.class})
    @ApiPostMapping("/save")
    public CustomApiResult<Boolean> saveStudent(@Valid @ApiRequestBody("学生信息") StudentInfoDto dto) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("ycsd_student_info");
        dynamicObject.set("name", dto.getName());
        dynamicObject.set("age", dto.getAge());
        CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(
                dynamicObject.getDataEntityType().getName()
                , dynamicObject
                , String.valueOf(RequestContext.get().getOrgId()));
        String number = CodeRuleServiceHelper.getNumber(codeRule, dynamicObject);
        dynamicObject.set("billno", number);
        SaveServiceHelper.save(new DynamicObject[]{ dynamicObject });
        return CustomApiResult.success(
                SaveServiceHelper.saveOperate(
                                "save"
                                , dynamicObject.getDataEntityType().getName()
                                , new DynamicObject[]{dynamicObject}
                                , OperateOption.create())
                        .isSuccess());
    }

}
